package com.dudu.access;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import core.util.PropertyUtil;

/**
 * Redis工具类
 * 
 * @author jack
 * 
 */
public class RedisUtil {

	private static final Logger log = core.util.Logger.getLogger(RedisUtil.class.getName());

	private static String SERVER = PropertyUtil.getInstance().getValueByKey("redis.server", "redis"); // redis服务器IP
	private static int PORT = Integer.valueOf(PropertyUtil.getInstance().getValueByKey("redis.port", "redis")); // redis端口号
	private static String AUTH = PropertyUtil.getInstance().getValueByKey("redis.auth", "redis"); // 访问密码
	private static int MAX_WAIT = Integer.valueOf(PropertyUtil.getInstance().getValueByKey("redis.max_wait", "redis")); // 等待可用连接的最大时间
	private static int TIMEOUT = Integer.valueOf(PropertyUtil.getInstance().getValueByKey("redis.timeout", "redis")); // 超时时间
	private static int MAX_TOTAL = Integer
			.valueOf(PropertyUtil.getInstance().getValueByKey("redis.max_total", "redis")); // 最大连接数
	private static int MAX_IDLE = Integer.valueOf(PropertyUtil.getInstance().getValueByKey("redis.max_idle", "redis")); // 最大空闲连接数

	private static String KEY_PREFIX = "dudu:open:"; // 主键的前缀 

	private static JedisPool jedisPool = null;

	/**
	 * redis过期时间,以秒为单位
	 */
	public final static int EXRP_HOUR = 60 * 60; // 一小时
	public final static int EXRP_DAY = 60 * 60 * 24; // 一天
	public final static int EXRP_MONTH = 60 * 60 * 24 * 30; // 一个月

	static {
		try {
			JedisPoolConfig config = new JedisPoolConfig();
			config.setMaxWaitMillis(MAX_WAIT);
			config.setMaxTotal(MAX_TOTAL);
			config.setMaxIdle(MAX_IDLE);
			jedisPool = new JedisPool(config, SERVER, PORT, TIMEOUT);
		} catch (Exception ex) {
			log.error("create JedisPool error:" + ex);
		}
	}

	public synchronized static Jedis getJedis() {
		Jedis jedis = null;
		try {
			if (jedisPool != null) {
				jedis = jedisPool.getResource();
			}
		} catch (Exception ex) {
			log.error("get jedis error:" + ex);
		}
		return jedis;
	}

	/**
	 * 释放jedis资源
	 * 
	 * @param jedis
	 */
	@SuppressWarnings("deprecation")
	public static void returnResource(final Jedis jedis) {
		if (jedis != null && jedisPool != null) {
			jedisPool.returnResource(jedis);
		}
	}

	/**
	 * 设置 String
	 * 
	 * @param key
	 * @param value
	 */
	public static int setString(String key, String value) {
		int result = -1;
		Jedis jedis = null;
		try {
			key = KEY_PREFIX + key;
			value = StringUtils.isBlank(value) ? "" : value;
			jedis = getJedis();
			if (jedis != null) {
				jedis.auth(AUTH);
				jedis.select(3);
				jedis.set(key, value);
				result = 1;
			}
		} catch (Exception ex) {
			log.error("Set key error : " + ex);
		} finally {
			if (jedis != null)
				jedis.close();
		}
		return result;
	}

	/**
	 * 设置 过期时间
	 * 
	 * @param key
	 * @param seconds
	 *            以秒为单位
	 * @param value
	 */
	public static void setString(String key, int seconds, String value) {
		Jedis jedis = null;
		try {
			key = KEY_PREFIX + key;
			value = StringUtils.isBlank(value) ? "" : value;
			jedis = getJedis();
			if (jedis != null) {
				jedis.auth(AUTH);
				jedis.select(3);
				jedis.setex(key, seconds, value);
			}
		} catch (Exception ex) {
			log.error("Set key error : " + ex);
		} finally {
			if (jedis != null)
				jedis.close();
		}
	}

	/**
	 * 获取String值
	 * 
	 * @param key
	 * @return value
	 */
	public static String getString(String key) {
		String value = "";
		Jedis jedis = null; 
		try {
			key = KEY_PREFIX + key;
			jedis = getJedis();
			jedis.auth(AUTH);
			jedis.select(3);
			if (jedis == null || !jedis.exists(key)) {
				return null;
			}
			value = jedis.get(key);
		} catch (Exception ex) {
			log.error("Get value error :" + ex);
		} finally {
			if (jedis != null)
				jedis.close();
		}
		return value;
	}
    
	/**
	 * 生成自增数字
	 * @param key
	 * @return
	 */
	public static long incr(String key) {
		long value = -1;
		Jedis jedis = null;
		try {
			key = KEY_PREFIX + key;
			jedis = getJedis();
			if (jedis == null) {
				return value;
			}
			jedis.auth(AUTH);
			jedis.select(3);
			value = jedis.incr(key);
		} catch (Exception ex) {
			log.error("incr error :" + ex);
		} finally {
			if (jedis != null)
				jedis.close();
		}
		return value;
	}
	
	/**
	 * 删除字符串类型的键
	 * @param key 键名字
	 * @return 
	 */
	public static long delKey(String key){
		long value = -1;
		Jedis jedis = null;
		try {
			key = KEY_PREFIX + key;
			jedis = getJedis();
			if (jedis == null) {
				return value;
			}
			jedis.auth(AUTH);
			jedis.select(3);
			value = jedis.del(key);
		} catch (Exception ex) {
			log.error("del error :" + ex);
		} finally {
			if (jedis != null)
				jedis.close();
		}
		return value;
	}
}
